import { contextBridge, ipcRenderer } from 'electron'

// We are exposing a controlled, type-safe API to the renderer.
// The types for `window.electronAPI` are defined
// globally in `src/vite-env.d.ts`, so no explicit import is needed here.

contextBridge.exposeInMainWorld('electronAPI', {
  /**
   * A generic task runner for all main process functions.
   * This is the single entry point for all main process interactions.
   * @param taskName - The name of the task to run, corresponding to a case in main.ts's 'run-task' handler.
   * @param args - Arguments for the task.
   * @returns A promise that resolves with the result of the task.
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  runTask: (taskName: string, ...args: any[]): Promise<any> => ipcRenderer.invoke('run-task', taskName, ...args),

  /**
   * Securely expose a listener for messages from the main process.
   * @param callback - The function to execute when a message is received.
   *                   It receives the event and message arguments.
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  onMainProcessMessage: (callback: (event: any, ...args: any[]) => void) => {
    ipcRenderer.on('main-process-message', callback);
  }
});
